import 'package:flutter/material.dart';
import 'package:flutter_ducafecat_news/base/base_mvp_presenter.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/utils/utils.dart';
import 'package:flutter_ducafecat_news/common/widgets/progress_dialog.dart';
import 'package:fluttertoast/fluttertoast.dart';

abstract class BaseMvpView {
  bool _isEmptyWidgetVisible = false;
  bool isDialogShow = false;
  bool _isLoadingWidgetShow = false;
  String dialogMsg = "加载中...";
  String _errorContentMesage = "网络错误啦~~~";
  String _errImgPath = "assets/images/load-error-image.png";
  String _emptyWidgetContent = "暂无数据~";
  String _emptyImgPath = "assets/images/empty-image.png"; //自己根据需求变更
  bool _isErrorWidgetShow = false; //错误信息是否显示
  State _stateBaseFunction;
  double _otherHeight = 0;
  BuildContext _context;
  void initBaseCommon(State state, BuildContext context) {
    _stateBaseFunction = state;
    _context = context;
  }

  Widget getBaseView(BuildContext context) {
    return Column(
      children: <Widget>[
        Container(
          width: getScreenWidth(_context),
          height: getScreenHeight(_context) -
              getAppBarHeight(_context) -
              getTopBarHeight(_context) -
              _otherHeight,
          color: Color(0xFFECECEC), //背景颜色，可自己变更
          child: Column(
            children: <Widget>[
              buildHeaderWidget(context),
              Expanded(
                child: Stack(
                  children: <Widget>[
                    buildWidget(context),
                    _getBaseErrorWidget(),
                    _getBaseEmptyWidget(),
                    _getBassLoadingWidget(),
                    _getLoadingView()
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  ///返回UI控件 相当于setContentView()
  Widget buildWidget(BuildContext context);

  ///返回UI控件 相当于setContentView()
  Widget buildHeaderWidget(BuildContext context) {
    return Container();
  }

  Widget _getLoadingView() {
    return Offstage(
      offstage: !isDialogShow,
      child: ProgressDialog(
        loading: true,
        msg: dialogMsg,
        child: Container(
          width: double.infinity,
          height: double.infinity,
        ),
      ),
    );
  }

  ///暴露的错误页面方法，可以自己重写定制
  Widget getErrorWidget() {
    return Container(
      color: Colors.white,
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Image.asset(
              _errImgPath,
              width: 150,
              height: 150,
            ),
            Container(
              margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
              child: Text(_errorContentMesage,
                  style: TextStyle(
                    fontWeight: FontWeight.w600,
                  )),
            ),
          ],
        ),
      ),
    );
  }

  ///点击错误页面后展示内容
  void onClickErrorWidget() {}

  Widget getLoadingWidget() {
    return Container(
      color: Colors.black12,
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child:
            // 圆形进度条
            new CircularProgressIndicator(
          strokeWidth: 4.0,
          backgroundColor: Colors.blue,
          // value: 0.2,
          valueColor: new AlwaysStoppedAnimation<Color>(Colors.white),
        ),
      ),
    );
  }

  Widget _getBaseErrorWidget() {
    return Offstage(
      offstage: !_isErrorWidgetShow,
      child: getErrorWidget(),
    );
  }

  Widget _getBassLoadingWidget() {
    return Offstage(
      offstage: !_isLoadingWidgetShow,
      child: getLoadingWidget(),
    );
  }

  Widget _getBaseEmptyWidget() {
    return Offstage(
      offstage: !_isEmptyWidgetVisible,
      child: getEmptyWidget(),
    );
  }

  Widget getEmptyWidget() {
    return Container(
      //错误页面中心可以自己调整
//      padding: EdgeInsets.fromLTRB(0, 0, 0, 200),
      color: Colors.white,
      width: double.infinity,
      height: double.infinity,
      child: Center(
        child: Container(
          alignment: Alignment.center,
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Image.asset(
                _emptyImgPath,
                width: 150,
                height: 150,
              ),
              Container(
                margin: EdgeInsets.fromLTRB(0, 10, 0, 0),
                child: Text(_emptyWidgetContent,
                    style: TextStyle(
                      fontWeight: FontWeight.w600,
                    )),
              )
            ],
          ),
        ),
      ),
    );
  }

  void setOtherHeight(double otherHeight) {
    _stateBaseFunction.setState(() {
      _otherHeight = otherHeight;
    });
  }

  ///loading弹窗
  void setLoadingVisible(bool isVisible, [String msg = "加载中..."]) {
    _stateBaseFunction.setState(() {
      isDialogShow = isVisible;
      dialogMsg = msg;
    });
  }

  ///设置错误提示信息
  void setErrorContent(String content) {
    if (content != null) {
      _stateBaseFunction.setState(() {
        _errorContentMesage = content;
      });
    }
  }

  ///设置错误页面显示或者隐藏
  void setErrorWidgetVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _stateBaseFunction.setState(() {
      if (isVisible) {
        //如果可见 说明 空页面要关闭啦
        _isEmptyWidgetVisible = false;
      }
      // 不管如何loading页面要关闭啦，
      _isLoadingWidgetShow = false;
      _isErrorWidgetShow = isVisible;
    });
  }

  ///设置空页面显示或者隐藏
  void setEmptyWidgetVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _stateBaseFunction.setState(() {
      if (isVisible) {
        //如果可见 说明 错误页面要关闭啦
        _isErrorWidgetShow = false;
      }

      // 不管如何loading页面要关闭啦，
      _isLoadingWidgetShow = false;
      _isEmptyWidgetVisible = isVisible;
    });
  }

  void setLoadingWidgetVisible(bool isVisible) {
    // ignore: invalid_use_of_protected_member
    _stateBaseFunction.setState(() {
      _isLoadingWidgetShow = isVisible;
    });
  }

  ///设置空页面内容
  void setEmptyWidgetContent(String content) {
    if (content != null) {
      // ignore: invalid_use_of_protected_member
      _stateBaseFunction.setState(() {
        _emptyWidgetContent = content;
      });
    }
  }

  ///设置错误页面图片
  void setErrorImage(String imagePath) {
    if (imagePath != null) {
      // ignore: invalid_use_of_protected_member
      _stateBaseFunction.setState(() {
        _errImgPath = imagePath;
      });
    }
  }

  //设置空页面图片
  void setEmptyImage(String imagePath) {
    if (imagePath != null) {
      // ignore: invalid_use_of_protected_member
      _stateBaseFunction.setState(() {
        _emptyImgPath = imagePath;
      });
    }
  }

  void showLoading(String msg);

  void dismissLoading();

  void showSuccess(data);

  BasePresenter initPresenter();

  String className();

  ///弹吐司
  void toast(String content) {
    Fluttertoast.showToast(
        msg: content,
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.red,
        textColor: Colors.white,
        fontSize: 16.0);
  }
}
